<?php
	
	namespace app\api\controller;
	
	use app\common\model\VioContent;
    use app\common\model\VioContentWord;
    use app\common\model\Words;
	use think\Db;
	use app\common\controller\Api;
	use think\Model;
	
	class EarlyWarning extends Api
	{
		protected $noNeedLogin = [];
		protected $noNeedRight = ['*'];
		
		/**
		 * @Author   : crafters@qq.com
		 * @DateTime : 2022-3-2 10:56
		 * @Introduce: 预警信息
		 * @throws \think\exception\DbException
		 */
		public function List()
		{
			
//			if (!$this->request->isGet()) {
//				$this->error('请求方法不允许', null, 405);
//			}
			
			$this->request->filter(['strip_tags', 'trim']);
			
			$id                  = $this->request->param('id', 0, 'trim');
			$filing_type         = $this->request->param('filing_type', 0, 'trim');
			$sensitive_cate_name = $this->request->param('sensitive_cate_name', '', 'trim');
			$date_type           = $this->request->param('date_type', 'all', 'trim');
            $wordlevel           = $this->request->param('level', 0, 'trim');
			
			
			$where = [];
			
			if ($sensitive_cate_name) {
				$where['category'] = $sensitive_cate_name;
			}
			
			if ($id) {
				$where['id'] = $id;
			}
			
			$vioContent = new VioContent();
			$field = 'id, created_at, main, title, link, category, word, right_word, snapshot, website_id';
			// $field = 'id, created_at, main, title, link, category, word, right_word, website_id';
            $WordsModel = new Words();
            $where['user_id'] = $this->auth->id;
			$listFind   = $vioContent->field($field)
			                         ->where($where);
			
			if ($date_type == 'day') {
				$listFind->whereTime('created_at', 'today');
			}
            $contentwordModel = new VioContentWord();
            if($wordlevel){
                $word_ids = $WordsModel->where(['level'=>$wordlevel])->column('id');
                $word_ids = implode(',',$word_ids);
                $vioContent_ids = $contentwordModel
                    ->whereIn('sensitive_words_id',$word_ids)
                    ->group('vio_content_id')
                    ->column('vio_content_id');
                $idsarray = implode(',',$vioContent_ids);
                $listFind->whereIn('id',$idsarray);
            }
			
			if ($filing_type) {
				$listFind->with(['website' => function ($query) use ($filing_type) {
					$query->where(['filing_type' => $filing_type]);
				}]);
			}
			
			$list = $listFind->paginate(10);
			
			
			$items = $list->items();
			

			foreach ($items as &$value){
                if(!empty($wordlevel)){
                   $value['word_level'] = $wordlevel;
                }else{
                    $word = trim($value['word']);
                    if ($word){
                        if (strpos($word, '|')){
                            $tempArr = explode('|', $word);
                            if (count($tempArr)>0){
                                $word = trim($tempArr[0]);
                            }
                        }
                        $wordLog = $WordsModel->where(['words'=>$word])->column('level');
                        if (count($wordLog)>0){
                            $value['word_level'] = $wordLog[0];
                        }
                    }else{
                        $value['word_level'] ='';
                    }
                }

			}
			
			$result = ["total" => $list->total(), "rows" => $items];
			
			$this->success('预警信息列表', $result);
			
		}
		
		
	}
